package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Department;
import cn.wolfcode.edu.domain.Permission;
import cn.wolfcode.edu.domain.Role;
import cn.wolfcode.edu.mapper.RoleMapper;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IRoleService;
import cn.wolfcode.edu.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by abb on 2018-07-09.
 */
@Service
public class RoleServiceImpl implements IRoleService{
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void deleteByPrimaryKey(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRelationWithPermissionByRoleId(id);

    }

    @Override
    public void insert(Role entity) {
        roleMapper.insert(entity);

        List<Permission> pers = entity.getPermissions();
        for(Permission per : pers){
            roleMapper.insertRelationWithPermission(entity.getId(),per.getId());
        }

    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Role entity) {
        roleMapper.updateByPrimaryKey(entity);


        roleMapper.deleteRelationWithPermissionByRoleId(entity.getId());

        List<Permission> pers = entity.getPermissions();
        for(Permission per : pers){
            roleMapper.insertRelationWithPermission(entity.getId(),per.getId());
        }


    }

    public PageResult query(QueryObject qo) {
        int total = roleMapper.queryForCount(qo);
        List<Role> rows = roleMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    @Override
    public List<Role> listAll() {
        return null;
    }

    @Override
    public List<Long> listIdsByEmployeeId(Long id) {
        return roleMapper.listIdsByEmployeeId(id);
    }
}
